﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Fishing.Dal;
using Fishing.Model;

namespace Fishing.Admin
{
    public partial class Roles : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //绑定数据
            BindData();
        }

        public void BindData()
        {
            var str = "";
            if (txtSerchWords.Text.Length > 0)
            {
                str=" and roleName like '%"+ txtSerchWords.Text + "%'";
            }

            var _dal = new AdminRoleDal();
            var alldata = _dal.GetRoleDataBy(str);

            tvMenu.Nodes.Clear();
            // 首先绑定子节点
            foreach (var adminRole in alldata)
            {
                tvMenu.Nodes.Add(new TreeNode(adminRole.roleName, adminRole.roleID));
                //如果存在子节点
            }
        }

        protected void ImgBtnCan_Click(object sender, ImageClickEventArgs e)
        {
            txtSerchWords.Text = "";
            BindData();
            Clie();
        }

        protected void ImgBtnSave_Click(object sender, ImageClickEventArgs e)
        {
            var loginuser = "";
            if (Session["UserID"] != null)
            {
                loginuser = Session["UserID"].ToString();
            }

            var _dal = new AdminRoleDal();
            var model = new admin_role()
            {
                roleID = Guid.NewGuid().ToString().Substring(24, 10),
                roleName = txtroleName.Text,
                title = txttitle.Text,
                order = _dal.GetNewOrder(),
                createBy = loginuser
            };

            // 提交
            if (txtroleID.Value.Length == 0)
            {
                var str = " and roleName='"+model.roleName+"'";
                if (_dal.GetRoleDataBy(str).Count > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "提示",
                        "<script type='text/javascript'>alert('角色已存在！');</script>");
                }
                else
                {
                    if (_dal.InsertDate(model))
                    {
                        //添加成功
                        ClientScript.RegisterStartupScript(this.GetType(), "提示",
                            "<script type='text/javascript'>alert('添加成功！');</script>");
                        txtroleID.Value = model.roleID;
                        BindData();
                    }
                    else
                    {
                        // 添加失败
                        ClientScript.RegisterStartupScript(this.GetType(), "提示",
                            "<script type='text/javascript'>alert('添加失败！');</script>");
                    }
                }

            }

                
            
            else
            {
                model.roleID = txtroleID.Value;
                model.updateBy = loginuser;
                if (_dal.UpdateDate(model))
                {
                    //修改成功
                    ClientScript.RegisterStartupScript(this.GetType(), "提示",
                        "<script type='text/javascript'>alert('修改成功！');</script>");
                    BindData();
                }
                else
                {
                    // 修改失败
                    ClientScript.RegisterStartupScript(this.GetType(), "提示",
                        "<script type='text/javascript'>alert('修改失败！');</script>");
                }
            }
        }

        protected void ImgBtnAdd_Click(object sender, ImageClickEventArgs e)
        {
            Clie();
        }

        public void Clie()
        {
            txtroleName.Text = "";
            txttitle.Text = "";
            txtroleID.Value = "";
        }

        protected void ImgbtnSelect_Click(object sender, ImageClickEventArgs e)
        {
            BindData();
            Clie();
        }

        protected void ImgBtnDel_Click(object sender, ImageClickEventArgs e)
        {
            var _dal = new AdminRoleDal();
            var roleid = txtroleID.Value;

            if (roleid.Length > 0)
            {
                if (_dal.IsRoleByUser(roleid))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "提示",
                        "<script type='text/javascript'>alert('该角色下还有用户未解绑，不能进行删除操作！');</script>");
                }
                else
                {
                    if (_dal.DelDate(roleid))
                    {
                        //删除成功
                        ClientScript.RegisterStartupScript(this.GetType(), "提示",
                            "<script type='text/javascript'>alert('删除成功！');</script>");
                        Clie();
                        BindData();

                    }
                    else
                    {
                        // 删除失败
                        ClientScript.RegisterStartupScript(this.GetType(), "提示",
                            "<script type='text/javascript'>alert('删除失败！');</script>");
                    }
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "提示",
                    "<script type='text/javascript'>alert('请先选择需要删除的角色！');</script>");
            }
        }

        protected void tvMenu_SelectedNodeChanged(object sender, EventArgs e)
        {
            var role= this.tvMenu.SelectedNode.Value;
            if (role.Length > 0)
            {
                var str = " and roleid='"+ role + "'";
                
                var _dal = new AdminRoleDal();
                var alldata = _dal.GetRoleDataBy(str);
                foreach (var adminRole in alldata)
                {
                    txtroleID.Value = adminRole.roleID;
                    txtroleName.Text = adminRole.roleName;
                    txttitle.Text = adminRole.title;
                }
            }

        }

    }
}